package com.wjc.train.common.handler;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wjc.train.common.exception.BusinessException;
import com.wjc.train.common.resp.CommonResp;
import io.seata.core.context.RootContext;
import jakarta.validation.ConstraintViolationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理、数据预处理等
 * @author Guessed
 */
@ControllerAdvice
public class ControllerExceptionHandler {

    private static final Logger LOG = LoggerFactory.getLogger(ControllerExceptionHandler.class);

    /**
     * 校验异常统一处理
     * @param e
     * @return
     */
    @ExceptionHandler(value = BindException.class)
    @ResponseBody
    public CommonResp validExceptionHandler(BindException e) {
        //构造业务返回
        CommonResp commonResp = new CommonResp();
        LOG.warn("参数校验失败：{}", e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
        commonResp.setSuccess(false);//设置返回值为false
        commonResp.setMessage(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
        return commonResp;
    }

    @ExceptionHandler(value = ConstraintViolationException.class)
    @ResponseBody
    public CommonResp validExceptionHandler(ConstraintViolationException e) {
        //构造业务返回
        CommonResp commonResp = new CommonResp();
        LOG.warn("参数校验失败：{}", e.getMessage());
        commonResp.setSuccess(false);//设置返回值为false
        commonResp.setMessage(e.getMessage());
        return commonResp;
    }

    /**
     * 校验异常统一处理
     * @param e
     * @return
     */
    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public CommonResp validExceptionHandler(BusinessException e) {
        CommonResp commonResp = new CommonResp();
        commonResp.setSuccess(false);
        if(ObjectUtil.isNotEmpty(e.getCode())){
            commonResp.setMessage(e.getCode().getDesc());
            LOG.warn("业务异常：{}", e.getCode().getDesc());
        }else{
            commonResp.setMessage(e.getMessage());
            LOG.warn("业务异常：{}", e.getMessage());
        }
        return commonResp;
    }

    /**
     * 校验异常统一处理
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public CommonResp validExceptionHandler(Exception e) throws Exception {
        LOG.info("seata全局事务ID:{}", RootContext.getXID());
        //如果是在一次全局事务里出现异常了，就不要包装返回值，将异常抛给调用方，让调用方回滚
        if(StringUtils.isNotBlank(RootContext.getXID())){
            throw e;
        }
        CommonResp commonResp = new CommonResp();
        LOG.error("系统异常：", e);
        commonResp.setSuccess(false);
        commonResp.setMessage("系统出现异常，请联系管理员");
        return commonResp;
    }
}
